package com.canbot.u05.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.bean.Audio;
import com.canbot.u05.glide.GlideApp;

import java.util.ArrayList;
import java.util.List;

/**
 * 导购模式选择音频Adapter
 */

public class ChooseAudioAdapter extends BaseAdapter {

        private Context context;

        private List<Audio> data;

        private LayoutInflater inflater;

        public ChooseAudioAdapter(Context context, List<Audio> data) {
                this.context = context;
                setData(data);
                inflater = LayoutInflater.from(context);

        }

        private void setData(List<Audio> data) {
                if (data == null) {
                        data = new ArrayList<>();
                }
                this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
                ViewHolder holder = null;
                if (convertView == null) {
                        holder = new ViewHolder();
                        convertView = inflater.inflate(R.layout.layout_shopping_mode_choose_audio, null);
                        holder.imageView = (ImageView) convertView.findViewById(R.id.item_iv_audio);
                        holder.fileName = (TextView) convertView.findViewById(R.id.shopping_item_tv_audio_num);
                        holder.checkBox = (CheckBox) convertView.findViewById(R.id.iv_shopping_mode_choose_audio);

                        convertView.setTag(holder);
                }
                else {
                        holder = (ViewHolder) convertView.getTag();
                }
                holder.imageView.setImageResource(R.drawable.audio_frequency);
                Audio audio = data.get(position);
                String w = audio.toString();
                Log.e("t", "" + w);
                holder.fileName.setText(audio.getName());
                String e = audio.getName();
                Log.e("t", "" + e);
                holder.checkBox.setChecked(audio.isChecked());

                return convertView;
        }

        @Override
        public int getCount() {
                return data.size();
        }

        @Override
        public Object getItem(int i) {
                return data.get(i);
        }

        @Override
        public long getItemId(int i) {
                return i;
        }

        private class ViewHolder {

                private ImageView imageView;

                private TextView fileName;

                private CheckBox checkBox;

        }

        /**
         * 更新控件显示
         *
         * @param posi
         * @param gridView
         */
        public void updateView(int posi, GridView gridView) {
                int visibleFirstPosi = gridView.getFirstVisiblePosition();
                int visibleLastPosi = gridView.getLastVisiblePosition();

                if (posi >= visibleFirstPosi && posi <= visibleLastPosi) {
                        View view = gridView.getChildAt(posi - visibleFirstPosi);
                        ViewHolder holder = (ViewHolder) view.getTag();
                        holder.checkBox.setChecked(data.get(posi).isChecked());
                }
                else {

                }
        }
}
